class Solution {
public:
    int widthOfBinaryTree(TreeNode* root) {
        int m,s,M = 0;
        queue<pair<TreeNode*, long>> n;
        n.emplace(root, 1);
        while(!n.empty()){
             m = n.size(), s = n.front().second;
            while(m--){
                auto [q, r] = n.front();
                n.pop();
                M = max(M, (int)(r-s+1));
                if(q->left)
                    n.emplace(q->left, 2*(r-s));
                if(q->right)
                    n.emplace(q->right, 2*(r-s)+1);
            }
        }
        return M;
    }
};